﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace C4F_Ocean
{
    /// <summary>
    /// A basic free-camera class
    /// </summary>
    public class Camera
    {
        // The eye position
        public Vector3 Position { get; private set; }
        // the rotation values
        private float pitch, yaw;

        /// <summary>
        /// How the camera is controlled
        /// </summary>
        public enum ControlType
        {
            Gamepad,
            MouseKeyboard,
        }

        /// <summary>
        /// Allows the application to choose between
        /// gamepad controls and PC mouse/keyboard
        /// setups
        /// </summary>
        public ControlType CurrentController { get; set; }

        public Camera()
        {
            Position = new Vector3(0,10,0);
            pitch = 0;
            yaw = 0;
#if XBOX
            CurrentController = ControlType.Gamepad;
#else
            CurrentController = ControlType.MouseKeyboard;
#endif
        }

        public void Update(GameTime gameTime)
        {
            if (CurrentController == ControlType.Gamepad)
            {
                GamePadState gps = GamePad.GetState(PlayerIndex.One);

                // rotate with the right thumbstick
                pitch += gps.ThumbSticks.Right.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
                yaw += gps.ThumbSticks.Right.X * (float)gameTime.ElapsedGameTime.TotalSeconds;

                Vector3 fwd = Vector3.Transform(new Vector3(1, 0, 0), Matrix.CreateRotationZ(pitch) * Matrix.CreateRotationY(yaw));
                Vector3 rgt = Vector3.Transform(new Vector3(0, 0, 1), Matrix.CreateRotationZ(pitch) * Matrix.CreateRotationY(yaw));
            }
            else
            {
                // query the devices
                MouseState ms = Mouse.GetState();
                KeyboardState kbs = Keyboard.GetState();

                // move based on how far off the mouse is from screen center
                yaw -= (ms.X - (Global.ScreenWidth / 2)) * 0.1f * (float)gameTime.ElapsedGameTime.TotalSeconds;
                pitch -= (ms.Y - (Global.ScreenHeight / 2)) * 0.1f * (float)gameTime.ElapsedGameTime.TotalSeconds;

                // reset the mouse to screen center
                Mouse.SetPosition(Global.ScreenWidth / 2, Global.ScreenHeight / 2);

                // generate the forward and right vectors
                // from the rotation values
                Vector3 fwd = Vector3.Transform(new Vector3(1, 0, 0), Matrix.CreateRotationZ(pitch) * Matrix.CreateRotationY(yaw));
                Vector3 rgt = Vector3.Transform(new Vector3(0, 0, 1), Matrix.CreateRotationZ(pitch) * Matrix.CreateRotationY(yaw));

                // move if the WASD keyboard keys are pressed
                Position += fwd * (kbs.IsKeyDown(Keys.W) ? 1 : kbs.IsKeyDown(Keys.S) ? -1 : 0) * 10 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                Position += rgt * (kbs.IsKeyDown(Keys.D) ? 1 : kbs.IsKeyDown(Keys.A) ? -1 : 0) * 10 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }

        /// <summary>
        /// Computes a view matrix for rendering
        /// </summary>
        /// <returns></returns>
        public Matrix GetViewMatrix()
        {
            Vector3 fwd = Vector3.Transform(new Vector3(1, 0, 0), Matrix.CreateRotationZ(pitch) * Matrix.CreateRotationY(yaw));
            return Matrix.CreateLookAt(Position, Position + fwd, Vector3.Up);
        }
    }
}
